/*
 * Copyright 2013 Sigurd Randoll <srandoll@digiway.de>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.digiway.rapidbreeze.shared.rest.download;

import de.digiway.rapidbreeze.shared.rest.IdentifiableObject;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URL;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Sigurd Randoll <srandoll@digiway.de>
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Download")
public class DownloadResource implements IdentifiableObject {

    private DownloadStatus downloadStatus;
    private Long filesize;
    private Long currentFilesize;
    private Double progress;
    private String targetFile;
    private String filename;
    private String provider;
    private Long eta;
    private Double bytesPerSecond;
    private URL url;
    private String identifier;
    private static final BigDecimal kb = new BigDecimal(1024);

    protected DownloadResource() {
    }

    private DownloadResource(Builder builder) {
        downloadStatus = builder.downloadStatus;
        targetFile = builder.targetFile;
        filename = builder.filename;
        provider = builder.provider;
        eta = builder.eta;
        url = builder.url;
        identifier = builder.identifier;
        filesize = builder.filesize;
        currentFilesize = builder.currentFilesize;
        progress = builder.progress;
        bytesPerSecond = builder.bytesPerSecond;
    }

    public void start() {
        downloadStatus = DownloadStatus.RUNNING;
    }

    public void pause() {
        downloadStatus = DownloadStatus.PAUSE;
    }

    public void waitState() {
        downloadStatus = DownloadStatus.WAITING;
    }

    public void resume() {
        downloadStatus = DownloadStatus.RUNNING;
    }

    public DownloadStatus getDownloadStatus() {
        return downloadStatus;
    }

    public Double getProgress() {
        return progress;
    }

    public boolean isWaiting() {
        return downloadStatus.equals(DownloadStatus.WAITING);
    }

    public boolean isError() {
        return downloadStatus.equals(DownloadStatus.ERROR);
    }

    public boolean isFinished() {
        return downloadStatus.equals(DownloadStatus.FINISHED_SUCCESSFUL);
    }

    public boolean isPause() {
        return downloadStatus.equals(DownloadStatus.PAUSE);
    }

    public boolean isRunning() {
        return downloadStatus.equals(DownloadStatus.RUNNING);
    }

    public String getTargetFile() {
        return targetFile;
    }

    public Long getFilesize() {
        return filesize;
    }

    public Long getCurrentFilesize() {
        return currentFilesize;
    }

    public String getFilename() {
        return filename;
    }

    public Long getEta() {
        return eta;
    }

    public Double getBytesPerSecond() {
        return bytesPerSecond;
    }

    public Double getKiloBytesPerSecond() {
        BigDecimal a = new BigDecimal(bytesPerSecond);
        return a.divide(kb, 2, RoundingMode.UP).doubleValue();
    }

    public String getCloudProvider() {
        return provider;
    }

    public URL getUrl() {
        return url;
    }

    @Override
    public String getIdentifier() {
        return identifier;
    }

    public static class Builder {

        private DownloadStatus downloadStatus;
        private String targetFile;
        private String filename;
        private Long filesize;
        private Long currentFilesize;
        private String provider;
        private Long eta;
        private Double bytesPerSecond;
        private URL url;
        private String identifier;
        private Double progress;

        public Builder identifier(String identifier) {
            this.identifier = identifier;
            return this;
        }

        public Builder targetFile(String targetFile) {
            this.targetFile = targetFile;
            return this;
        }

        public Builder filename(String value) {
            this.filename = value;
            return this;
        }

        public Builder progress(Double value) {
            this.progress = value;
            return this;
        }

        public Builder filesize(Long value) {
            this.filesize = value;
            return this;
        }

        public Builder currentFilesize(Long value) {
            this.currentFilesize = value;
            return this;
        }

        public Builder provider(String value) {
            this.provider = value;
            return this;
        }

        public Builder eta(Long value) {
            this.eta = value;
            return this;
        }

        public Builder bytesPerSecond(Double value) {
            this.bytesPerSecond = value;
            return this;
        }

        public Builder url(URL url) {
            this.url = url;
            return this;
        }

        public Builder downloadStatus(DownloadStatus status) {
            this.downloadStatus = status;
            return this;
        }

        public DownloadResource build() {
            return new DownloadResource(this);
        }
    }
}
